﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

/// <summary>
/// Summary description for Helper
/// </summary>
public static class Helper
{
    public static void MakeSeason()
    {
        Season season = DataBase.Seasons.GetCurrentSeason();

        for (int i = 17; i < 18; i++)
        {
            GameScraper.GetGamesForWeek(i, season);
        }
    }

    public static void CheckUser(this Page p)
    {
        if (p.Session["userID"] == null)
            p.Response.Redirect("Login.aspx");
    }

    public static void DecideSpreadGames()
    {
        Season s = DataBase.Seasons.GetCurrentSeason();
        for (int i = 1; i < 17; i++)
        {
            Week w = DataBase.Weeks.GetWeek(i.ToString(), s);
            var games = DataBase.Games.GetGamesForWeek(w);

            Game lastGame = games.Last();
            lastGame.HasSpread = true;

            DataBase.Games.UpdateGame(lastGame);
        }
    }

    public static void GetWinners()
    {
        Season s = DataBase.Seasons.GetCurrentSeason();
        for (int i = 1; i < 10; i++)
        {
            Week w = DataBase.Weeks.GetWeek(i.ToString(), s);

            ProjectWinner(w);
        }
    }

    public static List<UserRanking> LookForLosers(List<UserRanking> weekRankings, Week week)
    {
        int winningRecord = 0;
        int gamesLeft = DataBase.Weeks.GetNumberOfGamesLeft(week.ID.ToString());

        foreach (UserRanking rank in weekRankings)
        {
            if (winningRecord <= rank.Wins)
                winningRecord = rank.Wins;
        }

        var losing = (from u in weekRankings
                      where u.Wins < winningRecord
                      select u);

        var winning = (from u in weekRankings
                      where u.Wins == winningRecord
                      select u);

        foreach (var u in winning)
            u.Winner = null;

        foreach (var u in losing)
        {
            if (winningRecord - u.Wins > gamesLeft)
                u.Winner = false;
            else
                u.Winner = null;
        }

        return weekRankings;
    }
    public static void ProjectWinner(Week week)
    {
        int gamesLeft = DataBase.Weeks.GetNumberOfGamesLeft(week.ID.ToString());
        List<UserRanking> weekRankings = DataBase.Rankings.GetWeekStandings(week.ID.ToString());

        bool hasWinner = (from w in weekRankings
                          where w.Winner == true
                          select w).Count() > 0;
        if (hasWinner)
            return;

            int winningRecord = 0;
            foreach (UserRanking rank in weekRankings)
            {
                if (winningRecord <= rank.Wins)
                {
                    winningRecord = rank.Wins;
                    rank.Winner = null;
                }
                else
                {
                    rank.Winner = false;
                }
            }

            var surviviors = (from r in weekRankings
                              where r.Wins == winningRecord
                              select r);
            if (surviviors.Count() == 1)
            {
                UserRanking winningUser = surviviors.First();
                winningUser.Winner = true;
                winningUser.WinnerBySpread = false;
                if(gamesLeft == 0)
                DataBase.Weeks.DeclareWinner(week.ID.ToString(), winningUser.UserID.ToString(), false);

                new ChatHub().InsertGameEnd(string.Format("{0} HAS WON THE WEEK. ALL HAIL {0}!!",  winningUser.UserName), (int)week.ID);


            }
            else
            {
                if (gamesLeft == 0)
                {
                    int actualSpread = DataBase.Weeks.GetWeekSpread(week.ID.ToString());
                    int winningSpreadGuessDifference = 0;
                    List<UserRanking> Winners = new List<UserRanking>();

                    foreach (var s in surviviors)
                    {
                        int usersGuess = DataBase.Weeks.GetUsersWeekSpreadGuess(week.ID.ToString(), s.UserID.ToString());

                        int difference =Math.Abs(usersGuess - actualSpread);
                        if (winningSpreadGuessDifference == 0)
                        {
                            Winners.Add(s);
                            winningSpreadGuessDifference = difference;
                        }
                        else
                        {
                            if (winningSpreadGuessDifference >= difference)
                            {
                                if (winningSpreadGuessDifference > difference)
                                    Winners.Clear();

                                Winners.Add(s);
                                winningSpreadGuessDifference = difference;
                            }
                        }
                    }

                    foreach (UserRanking w in Winners)
                    {
                        w.Winner = true;
                        w.WinnerBySpread = false;
                        if (gamesLeft == 0)
                        DataBase.Weeks.DeclareWinner(week.ID.ToString(), w.UserID.ToString(), true);
                    }
                }
            }

    }

}